<?php

namespace app\api\controller;

use app\common\controller\Api;


/**
 * 刷题分类
 * @package app\api\controller
 */
class QuestionCate extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];



    /**
     * 获取分类列表
     * @return void
     */
    public function cate()
    {
        $type = $this->request->param('type', 'tree'); // tree=树形结构, first=一级分类, children=子分类
        $pid = $this->request->param('pid', 0); // 父级ID，用于查询子分类

        switch ($type) {
            case 'first':
                // 获取一级分类
                $cates = \app\common\model\QuestionCate::getFirstLevelCates();
                break;
            case 'children':
                // 根据父级ID获取子分类
                if ($pid <= 0) {
                    $this->error('父级ID不能为空');
                }
                $cates = \app\common\model\QuestionCate::getChildCates($pid);
                break;
            case 'tree':
                // 获取完整树形结构
                $cates = \app\common\model\QuestionCate::getAllCateTree();
                break;
            default:
                break;
        }
        $this->success('', $cates);
    }

    /**
     * 获取分类详情
     * @return void
     */
    public function cateDetail()
    {
        $id = $this->request->get('id');
        if (!$id) {
            $this->error('分类ID不能为空');
        }
        try {
            $cate = \app\common\model\QuestionCate::getCateById($id);
            if (!$cate) {
                $this->error('分类不存在');
            }
        } catch (\Exception $e) {
            $this->error('获取分类详情失败：' . $e->getMessage());
        }
        $this->success('', $cate);
    }

    /**
     * 获取分类路径
     * @return void
     */
    public function catePath()
    {
        $id = $this->request->get('id');

        if (!$id) {
            $this->error('分类ID不能为空');
        }

        try {
            $path = \app\common\model\QuestionCate::getCatePath($id);
            if (empty($path)) {
                $this->error('分类不存在');
            }
            $this->success('', [
                'path' => $path,
                'current' => end($path)
            ]);
        } catch (\Exception $e) {
            $this->error('获取分类路径失败：' . $e->getMessage());
        }
    }

    /**
     * 获取分类层级数据
     * @return void
     */
    public function cateLevel()
    {
        $level = $this->request->get('level', 1); // 1=一级, 2=二级, 3=三级

        try {
            switch ($level) {
                case 1:
                    // 获取一级分类
                    $cates = \app\common\model\QuestionCate::getFirstLevelCates();
                    break;

                case 2:
                    // 获取所有二级分类
                    $cates = \app\common\model\QuestionCate::where('pid', '>', 0)
                        ->where('status', 'normal')
                        ->order('weigh desc, id asc')
                        ->select();
                    break;

                case 3:
                    // 获取所有三级分类（需要先找到二级分类的ID）
                    $secondLevelIds = \app\common\model\QuestionCate::where('pid', '>', 0)
                        ->where('status', 'normal')
                        ->column('id');

                    if (empty($secondLevelIds)) {
                        $cates = [];
                    } else {
                        $cates = \app\common\model\QuestionCate::where('pid', 'in', $secondLevelIds)
                            ->where('status', 'normal')
                            ->order('weigh desc, id asc')
                            ->select();
                    }
                    break;

                default:
                    $this->error('层级参数错误');
            }

            $this->success('', $cates);
        } catch (\Exception $e) {
            $this->error('获取分类层级数据失败：' . $e->getMessage());
        }
    }
}
